package com.jixiantubu.wode;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import com.example.jixiantubu.R;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jixiantubu.adapter.Wo_canjiahuodongadapter;
import com.jixiantubu.bean.Ac_Bean;
import com.jixiantubu.util.MyApplication;
import com.jixiantubu.yueban.YueBan_XiangQing;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import android.annotation.SuppressLint;
import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

@SuppressLint("NewApi")
public class WocanjiaFragment extends Fragment {

	View woView;
	ListView wo_canjia_lv;
	List<Ac_Bean> list = new ArrayList<Ac_Bean>();
	private String url = "http://" + MyApplication.getIP()
			+ ":8080/Jinnang/YueBan_Middle";
	Context context;
	MyApplication application;
	Wo_canjiahuodongadapter adapter;
	private int number = 1;// 默认第一页
	private String username;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		username=getArguments().getString("username");
		Log.e("username----------00000", username);
		woView = inflater.inflate(R.layout.wo_jieban_canjia, null);
		initViews();
		initData();
		return woView;
	}

	private void initViews() {

		wo_canjia_lv = (ListView) woView.findViewById(R.id.wo_canjia_lv);
		wo_canjia_lv.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				Intent intent = new Intent(getActivity(),
						YueBan_XiangQing.class);
				intent.putExtra("ac", list.get(arg2));
				startActivity(intent);
			}
		});

	}

	private void initData() {
		adapter = new Wo_canjiahuodongadapter(list, getActivity());
		wo_canjia_lv.setAdapter(adapter);
		getList(username);

	}

	private void getList(String username) {
		
		// 绑定参数
		RequestParams params = new RequestParams();
		params.addQueryStringParameter("key", "7");
		params.addQueryStringParameter("username", username);
		Log.e("username----------111111111", username);
		HttpUtils httpUtils = new HttpUtils();
		httpUtils.send(HttpMethod.POST, url, params,
				new RequestCallBack<String>() {
					@Override
					public void onFailure(HttpException arg0, String arg1) {
						// 请求失败
					}

					@Override
					public void onSuccess(ResponseInfo<String> arg0) {
						// 请求成功
						String result = arg0.result;
						Gson gson = new Gson();
						Type type = new TypeToken<List<Ac_Bean>>() {
						}.getType();
						list = gson.fromJson(result, type);
						adapter.setList(list);
						adapter.notifyDataSetChanged();
					}

				});

	}

}
